//
//  PopoverAnimator.swift
//  firstSwiftWeiBo
//
//  Created by Inverno on 16/12/12.
//  Copyright © 2016年 Inverno. All rights reserved.
//

import UIKit

typealias ComoleteBlock = ((presented : Bool)-> (Void))?

class PopoverAnimator: NSObject {
    var isPresented = true
    var completeBlock : ComoleteBlock
    
    override init() {
        
    }
    
    init(completeBlock : ComoleteBlock) {
        self.completeBlock = completeBlock
    }
}

extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    
    // 目的：实现改变弹出控制器的大小
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let presentViewController = CustomPresentController(presentedViewController: presented, presentingViewController: presenting)
        
        return presentViewController
    }
    
    // 目的：自定义弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        if let completeBlock = completeBlock {
            completeBlock(presented: isPresented)
        }
        
        return self
    }
    
    // 目的：自定义消失动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        
        if let completeBlock = completeBlock {
            completeBlock(presented: isPresented)
        }
        
        return self;
    }
    
}

extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    
    // 动画执行时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    // 获取"转场上下文" 可以获取弹出的view和消失的view
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentedView(transitionContext) : animationForDismissedView(transitionContext)
        
    }
    
    // 自定义执行弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取弹出的view
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        presentView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        // 2.将view添加到containerView中
        transitionContext.containerView()?.addSubview(presentView)
        
        // 3.执行动画
        presentView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            presentView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                // 4.告诉上下文你已经完成了专场动画
                transitionContext.completeTransition(true)
        }
    }
    
    // 自定义消失动画
    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        dismissView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        
        // 2.执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
            }) { (_) -> Void in
                dismissView.removeFromSuperview()
                // 3.告诉上下文你已经完成了专场动画
                transitionContext.completeTransition(true)
        }
    }
    
}